package com.xf.sportplay.controller;

import com.alibaba.fastjson.JSON;
import com.xf.sportplay.dao.UserDao;
import com.xf.sportplay.entity.QueryInfo;
import com.xf.sportplay.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import sun.rmi.server.InactiveGroupException;

import java.util.HashMap;
import java.util.List;

@RestController
public class UserController {
    @Autowired
    private UserDao userDao;
    @RequestMapping("/alluser")
    public String getUserList(QueryInfo queryInfo){
        Integer numbers = userDao.getUserCounts("%"+queryInfo.getQuery()+"%");
        int pageStart = (queryInfo.getPageNum() - 1) * queryInfo.getPageSize();

        List<User> users = userDao.getAllUser("%"+queryInfo.getQuery()+"%",pageStart,queryInfo.getPageSize());
        HashMap<String,Object> res = new HashMap<>();
        res.put("numders",numbers);
        res.put("data",users);

        String userRes = JSON.toJSONString(res);
        return userRes;
    }
    @RequestMapping("/userstate")
    public String updataUserState(@RequestParam("id")Integer id,
                                  @RequestParam("state")Boolean state){
        int i = userDao.updateState(id,state);
        return i > 0 ? "success":"error";
    }

    @RequestMapping("/addUser")
    public String addUser(@RequestBody User user){
        user.setRole("普通用户");
        user.setState(false);
        int i = userDao.addUser(user);
        return i > 0 ?"success":"error";
    }

    @RequestMapping("/deleteUser")
    public String deleteUser(int id){
        int i = userDao.deleteUser(id);
        return i > 0 ? "success":"error";
    }

    @RequestMapping("/getUpdateUser")
    public String getUpdateUser(int id){
        User user = userDao.getUpdateUser(id);
        String str = JSON.toJSONString(user);
        return str;
    }

    @RequestMapping("/editUser")
    public String editUser(@RequestBody User user){
        int i = userDao.editUser(user);
        return i > 0 ? "success":"error";

    }

}
